package androidx.core.app;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import java.util.ArrayList;
import java.util.Iterator;

public final class h implements Iterable<Intent> {

    /* renamed from: b  reason: collision with root package name */
    private final ArrayList<Intent> f538b = new ArrayList<>();

    /* renamed from: c  reason: collision with root package name */
    private final Context f539c;

    public interface a {
        Intent d();
    }

    private h(Context context) {
        this.f539c = context;
    }

    public static h d(Context context) {
        return new h(context);
    }

    public h a(Intent intent) {
        this.f538b.add(intent);
        return this;
    }

    public h b(Activity activity) {
        Intent d2 = activity instanceof a ? ((a) activity).d() : null;
        if (d2 == null) {
            d2 = f.a(activity);
        }
        if (d2 != null) {
            ComponentName component = d2.getComponent();
            if (component == null) {
                component = d2.resolveActivity(this.f539c.getPackageManager());
            }
            c(component);
            a(d2);
        }
        return this;
    }

    public h c(ComponentName componentName) {
        int size = this.f538b.size();
        try {
            Context context = this.f539c;
            while (true) {
                Intent b2 = f.b(context, componentName);
                if (b2 == null) {
                    return this;
                }
                this.f538b.add(size, b2);
                context = this.f539c;
                componentName = b2.getComponent();
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("TaskStackBuilder", "Bad ComponentName while traversing activity parent metadata");
            throw new IllegalArgumentException(e);
        }
    }

    public void e() {
        f((Bundle) null);
    }

    public void f(Bundle bundle) {
        if (!this.f538b.isEmpty()) {
            ArrayList<Intent> arrayList = this.f538b;
            Intent[] intentArr = (Intent[]) arrayList.toArray(new Intent[arrayList.size()]);
            intentArr[0] = new Intent(intentArr[0]).addFlags(268484608);
            if (!b.f.d.a.e(this.f539c, intentArr, bundle)) {
                Intent intent = new Intent(intentArr[intentArr.length - 1]);
                intent.addFlags(268435456);
                this.f539c.startActivity(intent);
                return;
            }
            return;
        }
        throw new IllegalStateException("No intents added to TaskStackBuilder; cannot startActivities");
    }

    @Deprecated
    public Iterator<Intent> iterator() {
        return this.f538b.iterator();
    }
}
